import {
  fetchHome
} from '../../services/home/home';
import {
  fetchGoodsListHome
} from '../../services/good/fetchGoods';
import {
  http
} from '@/utils/request'
import Toast from 'tdesign-miniprogram/toast/index';

Page({
  data: {
    searchValue: '',
    imgSrcs: ['/asset/swiper1.jpg', '/asset/swiper.jpg'],
    tabList: [],
    goodsList: [],
    goodsListLoadStatus: 0,
    pageLoading: false,
    current: 1,
    autoplay: true,
    duration: '500',
    interval: 5000,
    navigation: {
      type: 'dots'
    },
    swiperImageProps: {
      mode: 'aspectFill'
    },
    message: '',
    marquee2: {
      speed: 60,
      loop: -1,
      delay: 0,
    },
  },

  goodListPagination: {
    index: 0,
    num: 10,
  },

  privateData: {
    tabIndex: 0,
  },

  onShow() {
    this.getTabBar().init();
    this.init();
    const userInfo = wx.getStorageSync('userInfo') || {}
    this.setData({
      userInfo
    })
    http('POST', '/messages/list').then(res => {
      const message = res.records[0].message
      this.setData({
        message
      })
    })
  },

  onReachBottom() {
    // if (this.data.goodsListLoadStatus === 0) {
    //   this.loadGoodsList();
    // }
  },

  onPullDownRefresh() {
    this.init();
  },

  init() {
    this.loadHomePage();
  },

  loadHomePage() {
    wx.stopPullDownRefresh();

    this.setData({
      pageLoading: true,
    });
    fetchHome().then(({
      swiper,
      tabList
    }) => {
      this.setData({
        tabList,
        // imgSrcs: swiper,
        pageLoading: false,
      });
      this.loadGoodsList(true);
    });
  },

  tabChangeHandle(e) {
    this.privateData.tabIndex = e.detail;
    this.loadGoodsList(true);
  },

  onReTry() {
    this.loadGoodsList();
  },

  async loadGoodsList(fresh = false) {
    if (fresh) {
      wx.pageScrollTo({
        scrollTop: 0,
      });
    }

    this.setData({
      goodsListLoadStatus: 1
    });

    const pageSize = this.goodListPagination.num;
    let pageIndex = this.privateData.tabIndex * pageSize + this.goodListPagination.index + 1;
    if (fresh) {
      pageIndex = 0;
    }

    try {
      const result = await fetchGoodsListHome(pageIndex, pageSize, this.data.searchValue, 1);
      const {
        records: nextList
      } = result
      nextList.forEach(item => {
        item.spuId = item.id
        item.title = item.title || item.name
        item.price = item.originalPrice
        item.thumb = item.imgUrl.split(',')[0]
      })
      console.log('nextList', nextList)
      this.setData({
        goodsList: fresh ? nextList : this.data.goodsList.concat(nextList),
        goodsListLoadStatus: 0,
      });

      this.goodListPagination.index = pageIndex;
      this.goodListPagination.num = pageSize;
    } catch (err) {
      console.log(err)
      this.setData({
        goodsListLoadStatus: 3
      });
    }
  },

  goodsThumbClick(e) {
    wx.navigateTo({
      url: `/pages/goods/details/index?spuId=${e.detail.goods.id}`,
    });
  },
  goodListClickHandle(e) {
    const {
      index
    } = e.detail;
    const {
      spuId
    } = this.data.goodsList[index];
    // if (this.data.userInfo.userType === 0) {
    wx.navigateTo({
      url: `/pages/compete/detail/detail?type=view&id=${spuId}&status=1`,
    });
    return
    // }
    // wx.navigateTo({
    //   url: `/pages/goods/details/index?spuId=${spuId}`,
    // });
  },

  goodListAddCartHandle() {
    Toast({
      context: this,
      selector: '#t-toast',
      message: '点击加入购物车',
    });
  },

  navToSearchPage() {
    wx.navigateTo({
      url: '/pages/goods/search/index'
    });
  },

  handleSubmit() {
    this.loadGoodsList(true);
  },

  navToActivityDetail({
    detail
  }) {
    // const { index } = detail;
    // const { spuId } = this.data.goodsList[index];
    // wx.navigateTo({
    //   url: `/pages/goods/details/index?spuId=${spuId}`,
    // });
    // const { index: promotionID = 0 } = detail || {};
    // wx.navigateTo({
    //   url: `/pages/promotion-detail/index?promotion_id=${promotionID}`,
    // });
  },
});